﻿using System.Collections.Generic;
using System.ServiceModel;
using Global.Impl.Server;
using Global.Interfaces.Server;
using Server.Users;
using Server.BuddyLists;
using Server.ChatLists;
using System.ServiceModel.Channels;
using System.Net;
using System;

namespace Server.Service
{
    [ServiceBehavior(Namespace = "sMessenger", IncludeExceptionDetailInFaults=true, InstanceContextMode = InstanceContextMode.Single)]
    public class sMessengerServer : IsMessengerServer
    {
        private static readonly object lockObject = new object();
        private IView _view;

        public sMessengerServer(IView view)
        {
            _view = view;
        }

        #region IsMessengerServer Members

        public List<Buddy> GetBuddyList(string username)
        {
            _view.AddToLog(username + " wanted to get buddylist (" + DateTime.Now.ToShortTimeString() + ")");
            return BuddyHandler.GetBuddies(username);
        }

        public List<ChatRequest> GetChatList(string username)
        {
            _view.AddToLog(username + " wanted to get chatlist (" + DateTime.Now.ToShortTimeString() + ")");
            lock (lockObject)
            {
                string ip = string.Empty;
                foreach (object o in OperationContext.Current.IncomingMessageProperties.Values)
                {
                    if (o is RemoteEndpointMessageProperty)
                    {
                        RemoteEndpointMessageProperty remoteEndpoint = o as RemoteEndpointMessageProperty;
                        IPAddress ipAddress = IPAddress.Parse(remoteEndpoint.Address);
                        if (remoteEndpoint != null)
                        {
                            ip = remoteEndpoint.Address;
                            break;
                        }
                    }
                }
                BuddyHandler.Buddyseen(username, ip);
            }
            return ChatHandler.GetChatRequestsForUser(username);
        }
        
        public void AddChat(string username, string buddyToChatTo, string chatGuid, string chatPassword)
        {
            _view.AddToLog(username + " added chat, and wanted to chat with: " + buddyToChatTo + " @ " + chatGuid + ", with password: " + chatPassword + " (" + DateTime.Now.ToShortTimeString() + ")");
            ChatHandler.AddChat(username, buddyToChatTo, chatGuid, chatPassword);
        }

        public bool Login(string username, string password, string port)
        {
            _view.AddToLog(username + " is logging in (" + DateTime.Now.ToShortTimeString() + ")");
            var returnValue = UserHandler.IsUserLoginValid(username, password);
            if (returnValue)
            {
                _view.AddToLog(username + " entered correct password (" + DateTime.Now.ToShortTimeString() + ")");
                BuddyHandler.Buddyseen(username, string.Empty, port);
            }
            else
                _view.AddToLog(username + " entered wrong password (" + DateTime.Now.ToShortTimeString() + ")");
            return returnValue;
        }
        #endregion
    }
}
